package com.ffapp.rainmonitorcustom.bean;


import com.alibaba.fastjson.annotation.JSONField;
import com.ffapp.rainmonitorcustom.basis.Constants;

import java.util.ArrayList;

import my.FileUtils;

public class HistoryListBean extends BaseListBean {
    
    public final static int TYPE_PROJECT = 0,TYPE_NEWS = 1,TYPE_CANDY = 2;

    public static final String FILE_CACHE = Constants.DIR_FILECACHE
            + "history.data";

    private int type;

    private ArrayList<BaseListData> list = new ArrayList<BaseListData>();

    @JSONField(name = "data")
    public ArrayList<BaseListData> getList() {
        return list;
    }

    @JSONField(name = "data")
    public void setList(ArrayList<BaseListData> list) {
        this.list = list;
    }

    public void addListBean(HistoryListBean listbean) {
        if (listbean == null)
            return;
        if (this.getCurrentPage() == INIT_PAGE) {
            setList(listbean.getList());
        } else {
            if (list == null)
                list = new ArrayList<BaseListData>();
            list.addAll(listbean.getList());
        }
        setListBeanData(listbean);
    }

    public HistoryListBean(int type) {
        this.type = type;
    }


    public static HistoryListBean getFromCache(int type) {
        HistoryListBean  data = (HistoryListBean) FileUtils.readObject(FILE_CACHE+type);
        if (data == null) {
            data = new HistoryListBean(type);
        }else {
            data.type = type;
        }

        return data;
    }

    public void save() {
        FileUtils.cacheObject(FILE_CACHE+type, this);
        // FileSaveHandler.saveObject(FILE_CACHE, this);
    }

    private static void delete(int type) {
        FileUtils.cacheObject(FILE_CACHE + type, null);
        // FileSaveHandler.removeObject(FILE_CACHE);
    }

    public void addSearchKey(String key){
        for(int i = 0 , l = list.size(); i < l ; i ++){
            if(key.equals(list.get(i).getName())){
                return;
            }
        }
        list.add(0,new BaseListData("",key));
        save();
    }

    public void deleteSearchKey(String key){
        for(int i = 0 , l = list.size(); i < l ; i ++){
            if(key.equals(list.get(i).getName())){
                list.remove(i);
                break;
            }
        }
        save();
    }
}
